package com.GlobalPaint.app.ui.Laborer;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.LinshiAdapter;
import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.LinshiWirkBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.codeBean;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


public class TemporaryWorkFragment extends Fragment {
    private LinshiAdapter adapter;
    private int page = 1;
    private XRecyclerView linshi_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private LinshiWirkBean bean;
    private List<LinshiWirkBean.DataBean> list;
    private codeBean bean2;
    private EditText ed_zhiwei;//地区
    private TextView tv_zousuo;
    private Dialog dlgWaiting2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_temporary_work, container, false);
        initView(view);
        return view;
    }
    private void initView(View view) {
        linshi_xrecyler = (XRecyclerView)view.findViewById(R.id.linshi_xrecyler);
        linearLayoutManager = new LinearLayoutManager(getContext());
        linshi_xrecyler.setLayoutManager(linearLayoutManager);
        ed_zhiwei = (EditText)view. findViewById(R.id.ed_zhiwei);
        tv_zousuo = (TextView)view. findViewById(R.id.tv_zousuo);
        tv_zousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(ed_zhiwei.getText().toString())){
                    getList();
                }else {
                    String zhiwei=ed_zhiwei.getText().toString();
                    sousuo(zhiwei);
                }
            }
        });
        getList();
        initLoad();
    }
    private void sousuo(String zhiwei){
        dlgWaiting2 = DialogWaiting.createDialogWaiting(getContext(), "加载中......");
        dlgWaiting2.show();
        String url= Constants.JASON_SERVICE_URL+"qlwjobtemphireinfo/listmy?page=1&location="+zhiwei
                +"&userid="+DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean=(LinshiWirkBean)o;
                if(bean.getStatus()==1){
                    if( bean.getData()!=null){
                        list=bean.getData();
                        adapter=new LinshiAdapter(list,TemporaryWorkFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }  else
                    {
                        list=new ArrayList<LinshiWirkBean.DataBean>();
                        adapter = new LinshiAdapter(list,TemporaryWorkFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                linshi_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                if(list!=null&&list.size()>0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    linshi_xrecyler.refreshComplete();

                }
            }
        });
    }
    private void getList() {
        String url= Constants.JASON_SERVICE_URL+"qlwjobtemphireinfo/listmy?page=1&userid="+DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(LinshiWirkBean)o;
                if(bean.getStatus()==1){
                    if( bean.getData()!=null){
                        list=bean.getData();
                        adapter=new LinshiAdapter(list,TemporaryWorkFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }  else
                    {
                        list=new ArrayList<LinshiWirkBean.DataBean>();
                        adapter = new LinshiAdapter(list,TemporaryWorkFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                linshi_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

                if(list!=null&&list.size()>0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    linshi_xrecyler.refreshComplete();

                }
            }
        });
    }
    public void job(){
        String url= Constants.JASON_SERVICE_URL+"qlwjobtemphireinfo/listmy?page=1&userid="+DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                if(list!=null)
                    list.clear();
                bean=(LinshiWirkBean)o;
                if(bean.getStatus()==1){
                    if( bean.getData()!=null){
                        list=bean.getData();
                        adapter=new LinshiAdapter(list,TemporaryWorkFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }  else
                    {
                        list=new ArrayList<LinshiWirkBean.DataBean>();
                        adapter = new LinshiAdapter(list,TemporaryWorkFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                linshi_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

                if(list!=null&&list.size()>0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    linshi_xrecyler.refreshComplete();

                }
            }
        });
    }
    public void jiedan(final int state, int id){

        String url= Constants.JASON_SERVICE_URL+"qlwjobtemphireinforecord/update?userid="+ DataManager.userEntity.getUserId()
                +"&hireInfoid="+id+"&state="+state;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), codeBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {

                bean2=(codeBean)o;
                if(bean2.getStatus()==1){
                    if(state==2){
                        Toast.makeText(getContext(),"取消接单",Toast.LENGTH_LONG).show();
                    }else {
                        Toast.makeText(getContext(),"接单成功",Toast.LENGTH_LONG).show();
                    }
                    job();
                }else {
                    if(state==2){
                        Toast.makeText(getContext(),"取消失败",Toast.LENGTH_LONG).show();
                    }
                    Toast.makeText(getContext(),"接单失败",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(ReturnStatusResultEntity error) {

            }
        });

    }
    private void initLoad() {
        linshi_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url;
                if(TextUtils.isEmpty(ed_zhiwei.getText().toString())){
                    url =  Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/listmy?page=" + 1
                            +"&userid="+DataManager.userEntity.getUserId();
                }else {
                    String zhiwei=ed_zhiwei.getText().toString();
                    url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/listmy?page=" + 1 +
                            "&location="+zhiwei +"&userid="+DataManager.userEntity.getUserId();
                }
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        LinshiWirkBean Jobbean = (LinshiWirkBean) o;
                        if (Jobbean.getStatus() == 1) {
                            if (Jobbean.getData() != null && Jobbean.getData().size() > 0) {
                                list.clear();
                                list.addAll(Jobbean.getData());
                                adapter.notifyDataSetChanged();
                                linshi_xrecyler.loadMoreComplete();
                            }
                        }
                        linshi_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        linshi_xrecyler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
                page++;
                String url;
                if(TextUtils.isEmpty(ed_zhiwei.getText().toString())){
                    url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/listmy?page=" + page
                            +"&userid="+DataManager.userEntity.getUserId();
                }else {
                    String zhiwei=ed_zhiwei.getText().toString();
                    url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/listmy?page=" + page+
                            "&location="+zhiwei +"&userid="+DataManager.userEntity.getUserId();;
                }

                DataManager.getInst().getHttpRequestJsonClass(url,DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        LinshiWirkBean jobBean = (LinshiWirkBean) o;
                        if (jobBean.getStatus() == 1) {
                            if (jobBean.getData() != null && jobBean.getData().size() > 0) {
                                list.addAll(jobBean.getData());
                            }
                        } else {
                            page = page - 1;
                            Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        }
                        linshi_xrecyler.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        page = page - 1;
                        Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        linshi_xrecyler.loadMoreComplete();
                    }
                });
            }
        });
    }

}
